<?php
namespace app\home\model;
use \think\Db;

class GoodscatsModel {

    public function getGoodsCats(){
        $where['is_del']= 0;
        $rows = Db::table('goods_type')->where($where)->select();
        $res=$this->showTreetwo($rows);
        $databbb=array();
        $dataaaa=array();
        foreach ( $res  as  $v  ){
            foreach ( $v['data']  as $b  ){
                $data['goods_cat_id2']=$b['id'];
                $data['is_del']=0;
                $rows2 = Db::table('goods')->where($data)->select();
                $b['child'] = $rows2;
                $dataaaa[] =$b;
            }
            $v['data'] = $dataaaa;
            $databbb[] = $v;
            $dataaaa=array();
        }
        return $databbb;
    }

    //无限极分类
    public function showTreeone($arr, $html = "&nbsp;&nbsp;&nbsp;&nbsp;", $pid = 0, $level = 0){
        static $newArr = array();
        foreach($arr as $key => $v){
            if($v['parentId'] == $pid) {
                $v['level'] = $level + 1;
                $html2 = ($level == 0) ? '' : '|--';//����Ŀ¼|--
                $v['html'] = str_repeat($html, $level) . $html2;
                $v['catName'] = $v['html'] . $v['catName'];
                $newArr[] = $v;

                $this->showTreeone($arr, $html , $v['catId'], $v['level']);
            }
        }
        return $newArr;
    }
    //无限极分类
    public function showTreetwo($arr, $html = "&nbsp;&nbsp;&nbsp;&nbsp;", $pid = 0, $level = 0){
        $newArr = array();
        foreach($arr as $key => $v){
            if($v['parent_id'] == $pid) {
                $v['level'] = $level + 1;
                $v['html'] = str_repeat($html, $level) ;
                $v['data'] = $this->showTreetwo($arr, $html , $v['id'], $v['level']);
                $newArr[] = $v;
            }
        }
        return $newArr;
    }
    public function searchGoods($goods_name){
        $where='goods_name like "%'.$goods_name.'%"  and is_del=0';
        $rows2 = Db::table('goods')->where($where)->select();
        return  $rows2;
    }

}